package com.android.settings.audio.peq.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ShareprefenceTool {
	public static ShareprefenceTool mst;
	public static String SETTING = "OPTION";
	private Map<String,Object> objectMap = new HashMap<>();
	static public ShareprefenceTool getInstance() {
		// TODO Auto-generated constructor stub
		if (mst==null) {
			synchronized (ShareprefenceTool.class) {
				if (mst == null) {
					mst = new ShareprefenceTool();
					return mst;
				}
			}
		}
		return mst;
	}

	public void setStringSet(String key, Set<String> values, Context mContext) {
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		Editor et = sp.edit();
		et.putStringSet(key, values);
		et.commit();
	}

	public Set<String> getStringSet(String key, Context mContext) {
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		Set<String> mSet = sp.getStringSet(key, null);
		return mSet;
	}

	public String[] getSringArraySharedPreference(String key, Context mContext) {
		String regularEx = "#";
		String[] str = null;
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		String values;
		values = sp.getString(key, "");
		str = values.split(regularEx);
		return str;
	}

	public void setSringArraySharedPreference(String key, String[] values,
			Context mContext) {
		String regularEx = "#";
		String str = "";
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		if (values != null && values.length > 0) {
			for (String value : values) {
				str += value;
				str += regularEx;
			}
		}
		Editor et = sp.edit();
		et.putString(key, str);
		et.commit();
	}

	public void setSringArray2(String key, List<String> values, Context mContext) {
		String regularEx = "#";
		String str = "";
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		if (values != null && values.size() > 0) {
			for (String value : values) {
				str += value;
				str += regularEx;
			}
		}
		Editor et = sp.edit();
		et.putString(key, str);
		et.commit();
	}

	public List<String> getSringArray2(String key, Context mContext) {
		String regularEx = "#";
		List<String> lists = new ArrayList<String>();
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		String values;
		values = sp.getString(key, "");

		String[] str = values.split(regularEx);
		for (int i = 0; i < str.length; i++) {
			lists.add(str[i]);
		}

		return lists;
	}

	public boolean getBooleanShareprefence(String key, Context mContext,
			boolean default_value) {
		boolean b;
		if (mContext==null){
			return default_value;
		}
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);

		b = sp.getBoolean(key, default_value);
		return b;
	}

	public String getStringShareprefence(String key, Context mContext,
			String default_value) {
		Object o = objectMap.get(key);
		if (o!=null){
			return (String) o;
		}
		String b;
		if (mContext==null){
			Log.e("", "getStringShareprefence:Context==null ");
			return default_value;
		}
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		b = sp.getString(key, null);
		if (b==null){
			return default_value;
		}
		objectMap.put(key,b);
		return b;
	}

	public int getIntShareprefence(String key, Context mContext,
			int default_value) {
		Object o = objectMap.get(key);
		if (o!=null){
			return (int) o;
		}
		int b;
		if (mContext==null)return default_value;
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);

		b = sp.getInt(key, -1000);
		if (b==-1000){
			return default_value;
		}
		objectMap.put(key,b);
		return b;
	}

	public void setBooleanSharedPreference(String key, boolean values,
			Context mContext) {
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);

		Editor et = sp.edit();
		et.putBoolean(key, values);
		et.commit();

	}

	public void setStringSharedPreference(String key, String values,
			Context mContext) {
		objectMap.put(key,values);
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);

		Editor et = sp.edit();
		et.putString(key, values);
		et.commit();

	}

	public void setIntSharedPreference(String key, int values, Context mContext) {
		objectMap.put(key,values);
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		Editor et = sp.edit();
		et.putInt(key, values);
		et.commit();

	}
	public void setLongSharedPreference(String key, long values, Context mContext) {
		objectMap.put(key,values);
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);

		Editor et = sp.edit();
		et.putLong(key, values);
		et.commit();

	}
	public long getLongShareprefence(String key, Context mContext,
			long default_value) {
		Object o = objectMap.get(key);
		if (o!=null){
			return (long) o;
		}
		long b;
		if (mContext==null){
			return default_value;
		}
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);

		b = sp.getLong(key, -1000);
		if (b==-1000){
			return default_value;
		}
		objectMap.put(key,b);
		return b;
	}
	public float[] getflaotSharedPreference(String key, float[] values,
			Context mContext) {

		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		float[] f = new float[11];
		for (int i = 0; i < 11; i++) {

			if (values == null) {

				f[i] = sp.getFloat(key + i, (float) 0.0);

			} else {

				f[i] = sp.getFloat(key + i, values[i]);
			}
		}

		return f;
	}

	public void setflaotSharedPreference(String key, float[] values,
			Context mContext) {
		if (values != null) {
			SharedPreferences sp = mContext.getSharedPreferences(SETTING,
					Context.MODE_PRIVATE);

			Editor et = sp.edit();
			// et.putInt(key, values);
			for (int i = 0; i < 11; i++) {
				et.putFloat(key + i, values[i]);
				et.commit();
			}

		}

	}

	public List<Integer> getIntegerArray(String key, Context mContext) {

		String regularEx = "#";
		List<Integer> lists = new ArrayList<Integer>();
		if (mContext != null) {
			SharedPreferences sp = mContext.getSharedPreferences(SETTING,
					Context.MODE_PRIVATE);
			String values;
			values = sp.getString(key, "");

			String[] str = values.split(regularEx);
			for (int i = 0; i < str.length; i++) {
				if (!str[i].equals("")) {
					Integer decode = Integer.parseInt((str[i]));
					lists.add(decode);
				}
			}
		}
		return lists;

	}

	public void setIntegerArray(String key, List<Integer> values,
			Context mContext) {
		String regularEx = "#";
		String str = "";
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		if (values != null && values.size() > 0) {
			for (Integer value : values) {
				str += value;
				str += regularEx;
			}
		}
		Editor et = sp.edit();
		et.putString(key, str);
		et.commit();
	}
	public void deleteImportData(String key,Context mContext){
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		Editor et = sp.edit();
		et.remove(key);
		et.commit();
	}
	public void deleteMiaoshuData(String key,Context mContext){
		SharedPreferences sp = mContext.getSharedPreferences(SETTING,
				Context.MODE_PRIVATE);
		Editor et = sp.edit();
		et.remove(key);
		et.commit();
	}

	public void delete(String key, Context context) {
		SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
		sp.edit().remove(key).commit();
	}
}
